package com.fenfen.novel;

import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;

import com.fenfen.novel.fragment.NovelListFragment;
import com.fenfen.novel.utils.CommonUtil;

public class SearchNovelActivity extends BaseActivity implements SearchView.OnQueryTextListener {

  private NovelListFragment fragment;
  private SearchView searchView;

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_novel_list);

    initToolbar();
    replaceFragment();

    handleIntent(getIntent());
  }

  private void initToolbar() {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle(R.string.search);
  }

  private void handleIntent(Intent intent) {
    if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_SEARCH)) {
      String query = intent.getStringExtra(SearchManager.QUERY);
      if (TextUtils.isEmpty(query)) {
        return;
      }
      PreferenceManager.getDefaultSharedPreferences(this).edit().putString(CommonUtil.SEARCH_KEY, query).apply();
      handleQuery();
    }
  }

  private void handleQuery() {
    String query = PreferenceManager.getDefaultSharedPreferences(this).getString(CommonUtil.SEARCH_KEY, "");
    //TODO:searchkey 过滤非法字符
    if (TextUtils.isEmpty(query)) {
      Snackbar.make(this.findViewById(android.R.id.content), getString(R.string.please_input_query_str), Snackbar.LENGTH_SHORT).show();
      return;
    }
    fragment.setSearchKey(query);
  }

  private void replaceFragment() {
    Bundle bundle = new Bundle();
    bundle.putByte(NovelListFragment.EXTRA_DATA_TYPE, CommonUtil.NovelListType.TYPE_NOVEL_SEARCH);
    bundle.putString(NovelListFragment.EXTRA_SEARCH_KEY, null);
    fragment = (NovelListFragment) getSupportFragmentManager().findFragmentByTag("searchNovelFrag");
    if (fragment == null) {
      fragment = NovelListFragment.newInstance(bundle);

      FragmentManager fm = getSupportFragmentManager();
      fm.beginTransaction().replace(R.id.container, fragment, "searchNovelFrag").commit();
    }
  }

  @Override
  protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_search, menu);
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    searchView.requestFocus();
    searchView.setOnQueryTextListener(this);
    MenuItemCompat.expandActionView(searchMenuItem);
    searchView.setQueryHint(getString(R.string.search_hint));
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    switch (item.getItemId()) {
      case android.R.id.home:
        super.onBackPressed();
        break;
//      case R.id.action_search:
//        onSearchRequested();
//        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override
  protected void onPause() {
    super.onPause();
    //clear focus
    if(searchView != null) {
      searchView.clearFocus();
    }
  }

  @Override
  public boolean onQueryTextSubmit(String query) {
    searchView.clearFocus();
    return false;
  }

  @Override
  public boolean onQueryTextChange(String newText) {
    return false;
  }
}
